<?php
session_start();
//set our name for this assessment- usually learner details would come from the database after they had logged in. This is hardcoded here for clarity.

define('EXAMINEE_ID', 400000015);
define('EXAMINEE_NAME', 'Test Examinee');

define('MAX_ITEMS', 10);


if (isset($_POST['cmdSubmit'])) {

    //data has been posted back, so check which radio button was selected.
    $userResponse = isset($_POST['itemOptions']) ? $_POST['itemOptions'] : null;

    $bOK = IsValid($userResponse, 1, 7);
    $optionSelected = null;
    if ($bOK) {
        $optionSelected = "<p>Your response to the previous question was: " . $userResponse . "</p>";
    } else {
        echo "<p>There was a problem with the response given.</p>";
    }
}
$_SESSION['CurrentItem'] = 0;
if (isset($_SESSION['CurrentItem'])) {
    $currentItem = $_SESSION['CurrentItem'];
    $currentItem++;
} else {
    $_SESSION['CurrentItem'] = 0;
    //set the current item number- if we have not already set the session var, then we are just starting the test, so should set it to our first item
    $currentItem = isset($_SESSION['CurrentItem']) ? $_SESSION['CurrentItem'] = $_SESSION['count'] + 1 : 0;
}

if ($currentItem > MAX_ITEMS) {
    header('Location: end_test.php');
    die();
}
$_SESSION['CurrentItem'] = $currentItem;


function IsValid($str, $min, $max)
{
    //args: a string to validate with its minimum and maximum length of the string    //names will initially come from a name submitted in the form
    $regex = '/^[A-Za-z0-9]{' . $min . ',' . $max . '}$/'; // this is then stored in a session variable
    return preg_match($regex, $str);
}


function QueryDB($linkIdentifier, $sqlString)
{
    $r = null;
    //use the $res var to store the result of the query
    $res = mysqli_query($linkIdentifier, $sqlString);

    //now get results set back

    if (!$res) {
        // nothing returned
        return false;
    }
    //if we get here, then the query worked, but did it return any rows?
    $numberOfRows = mysqli_num_rows($res);

    if ($numberOfRows > 0) {
        $r = array();

        //we iterate over $row in order to populate our own array that we then return to the calling code
        while ($row = mysqli_fetch_array($res)) {
            $r[] = $row;
        }

    } else {
        // no results found
        // $r will be returned with the default value of null so nothing to add here.

    }
    //clean up
    mysqli_free_result($res);
    return $r;
}


// rule: we will allow examinees to go back and forwards through the test (but you do not have to and for some tests you must not allow this)
function GetItem()
{

    $itemDetails = null;


	$host = "localhost";
	$user = "lr12aas";
  	$pass = "u3CJFB4r";
  	$db = "dblr12aas";
	
	$mysqli = new mysqli($host, $user, $pass, $db);
    if (mysqli_connect_errno()) {
 
//connection problem
	echo "<p class='crying'>Connection problem</p>";
	return false;} 
	else {
	return $mysqli;
   if ($mysqli) {
        $sqlStatement = "SELECT itemID, stem, option1, option2, option3, option4, option5"
            . " FROM item WHERE ItemID = "
            . $_SESSION['CurrentItem'];


        $itemDetails = QueryDB($mysqli, $sqlStatement);


    } else {
    }

    return $itemDetails;
}
}  
    
?>
<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
<link rel="stylesheet" type="text/css" href="css/assessment.css">
<title>Item template</title>
</head>

<body>
<?php
    
        //need to get the data returned from the fnc
        $item = GetItem();

        if ($item) {
            $itemID = $item[0][0];
            $itemStem = $item[0][1];
            $option1 = $item[0][2];
            $option2 = $item[0][3];
            $option3 = $item[0][4];
            $option4 = $item[0][5];

        } else {
		echo "<p class='crying'>Could not get item</p>";			
        }
        ?>
        <div id="root">
  <div id="mainContent">

    <h3>Progress</h3>
    <h4>
      <?php $displayItemNum = $_SESSION['CurrentItem']; ?>
      Question <?php echo $displayItemNum . " / " . MAX_ITEMS ?> </h4>
    <progress value="<?php echo $_SESSION['CurrentItem'] ?>" max="10"><?php echo $_SESSION['CurrentItem'] ?></progress>
    <p><?php echo $itemStem ?></p>
    <?php echo isset($optionSelected) ? $optionSelected : "" ?>
    <form id="showItem" id="showItem" action="item_template.php" method="POST">
    <div>
      <input name="itemOptions" id="radio1" type="radio" value="option1">
      <label id="label1" for="radio1"> <?php echo $option1 ?> </label>
    </div>
    <div>
      <input name="itemOptions" id="radio2" type="radio" value="option2">
      <label id="option2" for="radio2"> <?php echo $option2 ?> </label>
    </div>
    <div>
      <input name="itemOptions" id="radio3" type="radio" value="option3">
      <label id="option3" for="radio3"> <?php echo $option3 ?> </label>
    </div>
    <div>
      <input name="itemOptions" id="radio4" type="radio" value="option4">
      <label id="option4" for="radio4"> <?php echo $option4 ?> </label>
    </div>
    <div>
      <input name="itemOptions" id="radio5" type="radio" value="option5">
      <label id="option5" for="radio5"> <?php echo $option5 ?> </label>
    </div>
    <input name="cmdSubmit" id="cmdSubmit" type="submit" value="Check your answer">
    </form>
  </div>
</div>
</body>
</html>
